package com.example.index.controller;

import com.example.index.entity.*;
import com.example.index.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公开的控制器，所有人都可以访问
 */
@Controller
public class PublicController {
    @Autowired
    private UserService userService;

    @Resource
    private TeacherService teacherService;

    @Autowired
    private HomeWorkService homeWorkService;

    @Autowired
    IndexImageService indexImageService;

//    映射所有页面
    @GetMapping("{pathName}")
    public String index(@PathVariable String pathName) {
        return pathName;
    }

    @GetMapping("homework")
    public String homeWork(ModelMap modelMap){
        Page<HomeWork> homeWork = homeWorkService.queryByPage(new HomeWork(), PageRequest.of(0, 1000));
        List<HomeWork> homeWorkList = homeWork.getContent();

        List<HomeWork> graphicDesign = homeWorkList.stream().filter(h -> h.getWorksStyle() == 1).collect(Collectors.toList());//平面设计
        List<HomeWork> electronicBusiness = homeWorkList.stream().filter(h -> h.getWorksStyle() == 2).collect(Collectors.toList());//电子商务
        List<HomeWork> architecturalDecoration = homeWorkList.stream().filter(h -> h.getWorksStyle() == 3).collect(Collectors.toList());//建筑装饰
        List<HomeWork> gameAnimation = homeWorkList.stream().filter(h -> h.getWorksStyle() == 4).collect(Collectors.toList());//动漫设计
        List<HomeWork> networkMarketing = homeWorkList.stream().filter(h -> h.getWorksStyle() == 5).collect(Collectors.toList());//网络营销
        List<HomeWork> eSports = homeWorkList.stream().filter(h -> h.getWorksStyle() == 6).collect(Collectors.toList());//电子竞技

        modelMap.addAttribute("graphicDesign",graphicDesign);//平面设计
        modelMap.addAttribute("electronicBusiness",electronicBusiness);//电子商务
        modelMap.addAttribute("architecturalDecoration",architecturalDecoration);//建筑装饰
        modelMap.addAttribute("gameAnimation",gameAnimation);//动漫设计
        modelMap.addAttribute("networkMarketing",networkMarketing);//网络营销
        modelMap.addAttribute("eSports",eSports);//电子竞技
        return "homework";
    }

    @GetMapping({"index","/"})
    public String index(ModelMap modelMap){
        Page<IndexImage> indexImages = indexImageService.queryByPage(new IndexImage(), PageRequest.of(0, 1000));
        List<IndexImage> images = indexImages.getContent();

        List<IndexImage> professionList = images.stream().filter(indexImage -> indexImage.getImageStyle() == 1).collect(Collectors.toList());//专业图片集合
        List<IndexImage> problemList = images.stream().filter(indexImage -> indexImage.getImageStyle() == 2).collect(Collectors.toList());//问题图片集合
        List<IndexImage> jobList = images.stream().filter(indexImage -> indexImage.getImageStyle() == 3).collect(Collectors.toList());//就业服务集合
        List<IndexImage> guessList = images.stream().filter(indexImage -> indexImage.getImageStyle() == 4).collect(Collectors.toList());//猜你想学集合
        List<IndexImage> consultList = images.stream().filter(indexImage -> indexImage.getImageStyle() == 5).collect(Collectors.toList());//咨询图片集合
        List<IndexImage> schoolImageList = images.stream().filter(indexImage -> indexImage.getImageStyle() == 6).collect(Collectors.toList());//校园环境图片集合
        List<IndexImage> teacherImageList = images.stream().filter(indexImage -> indexImage.getImageStyle() == 7).collect(Collectors.toList());//教师风采图片集合

        modelMap.addAttribute("professionList",professionList);
        modelMap.addAttribute("problemList",problemList);
        modelMap.addAttribute("jobList",jobList);
        modelMap.addAttribute("guessList",guessList);
        modelMap.addAttribute("consultList",consultList);
        modelMap.addAttribute("schoolImageList",schoolImageList);
        modelMap.addAttribute("teacherImageList",teacherImageList);

        Page<Teacher> teachers = teacherService.queryByPage(new Teacher(), PageRequest.of(0, 100));
        List<Teacher> TeacherList = teachers.getContent();
        System.out.println(TeacherList);
        modelMap.addAttribute("teachers",TeacherList);

        return "index";
    }

    @PostMapping("/user")
    @ResponseBody//不找视图，直接返回json数据
    public User  add(User user) {
        System.out.println(user);
        User insert = this.userService.insert(user);
        return insert;
    }



}

